package com.model.action.student;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.model.action.BaseAction;
import com.model.service.admin.AdminService;
import com.model.service.student.StudentService;
import com.model.util.PageList;
import com.model.vo.Academy;
import com.model.vo.Admin;
import com.model.vo.Student;

@SuppressWarnings("serial")
public class StudentAction extends BaseAction implements SessionAware{

	 
		/*实体参数 声明
		 **********************************************/
	   Map session;
	 	Student student;   //实体类
		@Autowired
		StudentService studentService;  //业务层
		String resultView;   //视图页面
		PageList<Student> pageList;
		/*相关方法
		 * ********************************************/
	    @Override
	    public String execute() throws Exception {
	    	if(student==null){
	    		student = new Student();
	    		student.setId((Integer) session.get("nameId"));
	    	}
	    	if(session.get("role").equals("1")){
	    		student = studentService.QueryById(Student.class, student.getId());
	    		resultView ="/admin/student/StudentInfo.jsp";	
	    	}else{
	    		resultView ="/admin/student/addStudent.jsp";
	    	}
	    	return INPUT;
	    }
		/**
		 * 保存
		 * @return
		 */
		public String save(){
			if(student!=null){
				student.setPassword("123456");
				studentService.save(student);
			}
			 resultView ="/admin/student_queryList.action";
			 
			return "update";
		}
		/**
		 * 更新
		 * @return
		 */
	    @SuppressWarnings("unchecked")
		public String update(){
	    	if(student!=null){
	    		studentService.update(student);
			}
	    	if(session.get("role").equals("1")){
	    		resultView ="/admin/student.action";	
	    		session.put("name", student.getStuName());
	    	}else{
	    		 resultView ="/admin/student_queryList.action";
	    	}
			return "update";
		}
	    /**
	     * 删除
	     * @return
	     */
	   public String delete(){
		   if(student!=null){
			   studentService.delete(student);
			}
		   resultView ="/admin/student_queryList.action?pageList.pageNo=1";
			return "update";
		}
	   /**
	    * 分页查询
	    * @return
	    */
		public String queryList(){
		
	    	if(pageList==null){
	    		pageList = new PageList<Student>();	
	    	}
	    	String hql = "from Student "; 
	    	Object[] objects =null;
	    	if(pageList.getStuNo()!=null){
	    		 objects = new Object[1];
	    			objects[0] = "%"+pageList.getStuNo()+"%";
	    			hql += "where   stuNo like ?";
	    		}else if(pageList.getStuName()!=null){
	    			 objects = new Object[1];
	    			objects[0] = "%"+pageList.getStuName()+"%";
	    			hql += "where   stuName like ?";
	    		}
	    	
	    	pageList.setPageSize(15);
	    		int allrow = studentService.getAllRowCount(hql, objects);
	    		pageList.setRowCount(allrow);
	    		System.out.println(pageList.getStartOfPage()+","+allrow+","+pageList.getEndRow());
	    		
	    		pageList.setDataList(studentService.queryForPage(
	    		pageList.getStartOfPage(),
	    		pageList.getEndRow(),hql,objects));
			return SUCCESS;
		}
	    
	   
	    /**
	     * 通过Id查询
	     * @return
	     */
	   public String queryById(){
		   if(student==null){
			   student = new Student();
			   student.setId((Integer) session.get("nameId"));
			}	
		   student = studentService.QueryById(Student.class,student.getId());
		   
			return "edit";
		}
	   /***
	    * json查询
	    */
	   public void queryJsonList(){
		   if(pageList==null){
	    		pageList = new PageList<Student>();	
	    	}
		   if(student==null){
			   student = new Student();
		   }
	    	pageList.setPageSize(100);
	    		int allrow = studentService.getAllRowCount("from  Student where gradeClass = ?", new Object[]{student.getGradeClass()});
	    		pageList.setRowCount(allrow);
	    		System.out.println(pageList.getStartOfPage()+","+allrow+","+pageList.getEndRow());
	    		pageList.setDataList(studentService.queryForPage(
	    				pageList.getStartOfPage(), pageList.getEndRow(), "from Student where gradeClass = ?",new Object[]{student.getGradeClass()}));
	    		resultJson();
	   }
		/**
		 * 
		 * @param json
		 */
		private void resultJson() {
			try {
				HttpServletResponse response = ServletActionContext.getResponse();
				response.setContentType("text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				Gson g  = new Gson();
				String json = g.toJson(pageList);
		        PrintWriter pw = response.getWriter();
		        pw.print(json);
		        pw.flush();
		        pw.close();
			} catch (Exception e) {
				e.printStackTrace();
			}	

		}
		
	   /*相关参数注入
	  	 *********************************************/
		
		public String getResultView() {
			return resultView;
		}
		public Student getStudent() {
			return student;
		}
		public void setStudent(Student student) {
			this.student = student;
		}
		public StudentService getStudentService() {
			return studentService;
		}
		public void setStudentService(StudentService studentService) {
			this.studentService = studentService;
		}
		public void setResultView(String resultView) {
			this.resultView = resultView;
		}
		public PageList<Student> getPageList() {
			return pageList;
		}
		public void setPageList(PageList<Student> pageList) {
			this.pageList = pageList;
		}
		public Map getSession() {
			return session;
		}
		public void setSession(Map session) {
			this.session = session;
		}
		
			    
	}
